<?php

namespace app\erp\controller;

use app\erp\model\Fingerprintdb;
use app\erp\model\Sitedb;
use app\erp\model\Staffdb;
use app\erp\model\Departmentdb;
use app\erp\model\UFingerprintdb;
use app\erp\validate\Fingerprintrule;

class Staff extends Base
{
    protected $cols = array(
        array(
            'field' => 'staff_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'site_id_text',
            'width' => 160,
            'title' => '所属分站',
            'sort' => 'true'
        ),
        array(
            'field' => 'department_id',
            'width' => 160,
            'title' => '所属部门',
            'sort' => 'true'
        ),
        array(
            'field' => 'position_text',
            'width' => 160,
            'title' => '职位',
            'sort' => 'true'
        ),
        array(
            'field' => 'username',
            'width' => 160,
            'title' => '登录名',
            'sort' => 'true'
        ),
        array(
            'field' => 'realname',
            'width' => 160,
            'title' => '姓名',
            'sort' => 'true'
        ),
        array(
            'field' => 'order_search',
            'width' => 100,
            'title' => '订单搜索',
            'templet' => '#order_searchTpl'
        ),
        array(
            'field' => 'view_pre_order',
            'width' => 100,
            'title' => '未指派订单',
            'templet' => '#view_pre_orderTpl'
        ),
        array(
            'field' => 'sex',
            'width' => 100,
            'title' => '性别',
            'sort' => 'true'
        ),
        array(
            'field' => 'phone',
            'width' => 160,
            'title' => '联系电话',
            'sort' => 'true'
        ),
        array(
            'field' => 'tel',
            'width' => 160,
            'title' => '座机',
            'sort' => 'true'
        ),
        array(
            'field' => 'email',
            'width' => 160,
            'title' => '邮箱',
            'sort' => 'true'
        ),
        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '录入时间',
            'sort' => 'true'
        )
    );
    public $post;

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        if (isset ($this->post ['staff_id'])) {
            $this->post ['member_id'] = $this->post ['staff_id'];
            unset ($this->post ['staff_id']);
        }
        if (isset ($this->post ['_o'])) {
            $this->post ['_o'] = str_replace('_text', '', $this->post ['_o']);
        }
        // 调用分站数据
        $sitedb = new Sitedb ();
        $sitedb->param = array(
            '_o' => 'site_id',
            '_b' => 'asc',
            'page' => 1,
            'limit' => 1000
        );
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('get_site_list', $get_site_list ['data']);
    }

    public function index()
    {
        array_push($this->cols, array(
            'fixed' => 'right',
            'width' => 120,
            'toolbar' => '#staff_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxStaff()
    {
        $staffdb = new Staffdb ();
        $this->post ['is_staff'] = 'yes';
        $staffdb->param = $this->post;
        $staffdb->param ['is_supplier'] = 0;
        $return = $staffdb->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function ajaxMember()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $staffdb->param ['is_supplier'] = 0;
        $return = $staffdb->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function tplForm()
    {
        $member_id = '';
        $data = array();
        if (isset ($this->post ['member_id']) && !empty ($this->post ['member_id'])) {
            // 获取数据
            $staffdb = new Staffdb ();
            $staffdb->param = $this->post;
            $data = $staffdb->ajaxFind();
            unset ($data ['password']);
            $member_id = $this->post ['member_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('staff_id', $member_id);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $staffdb->param ['is_manage'] = 2;
        $return = $staffdb->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, -1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 获取单条数据
     */
    public function ajaxOneData()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $data = $staffdb->ajaxFind();
        $this->layuiReturn($data, 1);
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $staff = new Staffdb ();
        $staff->param = $this->post;
        $staff->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }

    public function ajaxRestore()
    {
        $staff = new Staffdb ();
        $staff->param = $this->post;
        $staff->ajaxRestore();
        $this->layuiReturn('数据恢复成功', 0);
    }

    // 回收站
    public function recycle()
    {
        array_push($this->cols, array(
            'field' => 'delete_time',
            'width' => 120,
            'title' => '删除时间'
        ), array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#recycle_staff_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function getCityDepartment()
    {
        $departmentdb = new Departmentdb ();

        $param = array(
            'site_id' => $this->post ['site_id'],
            '_o' => 'department_id',
            '_b' => 'desc',
            'page' => 1,
            'limit' => 30
        );
        $departmentdb->param = $param;
        $data = $departmentdb->ajaxSelect();
        $this->layuiReturn($data ['data'], $data ['count']);
    }

    /**
     * Title("指纹管理")
     */
    public function fingerprint($staff_id)
    {
        $fingerprintdb = new Fingerprintdb();
        $get_finger = $fingerprintdb->where('member_id', $staff_id)->select();
        $this->assign('get_finger', $get_finger);
        $this->assign('staff_id', $staff_id);
        return $this->fetch();
    }

    /**
     * Title("保存指纹")
     */
    public function saveFingerprint()
    {
        $fingerprintdb = new Fingerprintdb();
        $fingerprintrule = new Fingerprintrule();
        if (!$fingerprintrule->check($this->post)) {
            $this->error($fingerprintrule->getError());
        }
        $save_data = $this->post;
        $save_data['create_time'] = date('Y-m-d H:i:s');
        $fingerprintdb->save($save_data);
        $ufingerprintdb = new UFingerprintdb();
        $ufingerprintdb->save($save_data);
        $this->success('保存成功');
    }

    /**
     * Title("删除指纹")
     */
    public function delFingerprint()
    {
        $fingerprintdb = new Fingerprintdb();
        $ufingerprintdb = new UFingerprintdb();
        $get_finger = $fingerprintdb->find($this->post['id']);
        $get_finger->delete();
        $get_finger = $ufingerprintdb->find($this->post['id']);
        $get_finger->delete();
        $this->success('success');
    }

}